<?php
namespace app\libs;

use app\models\User;
use app\models\UserRoles;

/**
 * 系统辅助
 */
class Sys {

    static function isSuperUser($uid) {
        $userRoleId = (new User())->getColumn('role', $uid);
        $superRoleId = (new UserRoles())->getOneByCondition('id', "role_name='administrator'");

        return $userRoleId === $superRoleId['id'];
    }

    static function canPost($uid) {
        $openContribute = \Fate::$app->iniReader->get('system', 'open_contribute');

        if('1' === $openContribute) {
            return true;
        }

        // 没有开启投稿 那么只有会员有权限
        $m = new User();
        $role = $m->getColumn('role', $uid);

        if('' !== $role && 0 !== intval($role)) {
            return true;
        }

        return false;
    }

    static function canRegister() {
        $openRegister = \Fate::$app->iniReader->get('system', 'open_register');

        return '1' === $openRegister;
    }

    static function getPostExp() {
        $exp = \Fate::$app->iniReader->get('credit', 'experience_on_contribute');

        if(is_numeric($exp)) {
            return $exp;
        }

        return 0;
    }

    static function getCommentExp() {
        $exp = \Fate::$app->iniReader->get('credit', 'experience_on_comment');

        if(is_numeric($exp)) {
            return $exp;
        }

        return 0;
    }

    static function isOpenReview() {
        $openReview = \Fate::$app->iniReader->get('system', 'open_review');

        return '1' === $openReview;
    }

    static function getBannerUploadPath($uid) {
        $subdir = substr((string)$uid, 0, 1);

        return 'upload/banner/' . $subdir;
    }

    static function getArticleLengthConfig() {
        return [
            'min' => \Fate::$app->iniReader->get('system', 'article_min_length'),
            'max' => \Fate::$app->iniReader->get('system', 'article_max_length')
        ];
    }
}
